﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ProtoGame.GameLib.Entities
{
    public class StringSprite : GraphicEntity
    {
        SpriteFont font;

        public String Text { get; set; }

        public StringSprite(SpriteFont font)
        {
            this.font = font;

            Text = String.Empty;
        }

        /// <summary>
        /// Get a text from a string and format it to fit on the defined width and height.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="maxLineWidth"></param>
        /// <param name="maxLineHeight"></param>
        /// <returns>Return the rest of the text that did not fit.</returns>
        public void FitText(string text, float maxLineWidth, float maxTextHeight)
        {
            float spaceWidth = font.MeasureString(" ").X;
            float lineHeight = font.LineSpacing;

            string[] words = text.Split(' ');

            StringBuilder fit = new StringBuilder();
            //StringBuilder didntFit = new StringBuilder();

            float lineWidth = 0f;
            float textHeight = lineHeight;

            foreach (string word in words)
            {
                Vector2 wordSize = font.MeasureString(word);

                if (lineWidth + wordSize.X <= maxLineWidth)
                {
                    fit.Append(word).Append(" ");
                    lineWidth += wordSize.X + spaceWidth;
                }
                else if (textHeight + lineHeight <= maxTextHeight)
                {
                    fit.Append("\n").Append(word).Append(" ");
                    lineWidth = wordSize.X + spaceWidth;
                    textHeight += lineHeight;
                }
                else
                {
                    break;
                    // didntFit.Append(word).Append(" ");
                }
            }

            Text = fit.ToString();

            //return didntFit.ToString();
        }

        public override Rectangle Bounds
        {
            get
            {
                Vector2 size = MeasureString;
                return new Rectangle((int)Position.X, (int)Position.Y, (int)size.X, (int)size.Y);
            }
        }

        public Vector2 MeasureString
        {
            get { return font.MeasureString(Text); }
        }

        //public override void Update(GameTime gameTime) { }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.DrawString(font, Text, Position, ColorEffects, Rotation, Origin, Scale, Flip, LayerDepth);
        }

        /*public override bool DoCheckCollision(Entity otherEntity)
        {
            throw new NotImplementedException();
        }*/

        public override void DoCollision(byte collisionType)
        {
            // Sem colisão para Strings
        }
    }
}
